package Controller;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import View.MainFrame;

/**
 * Action implementation for the contact list
 * @author Vincent Francolin
 * @version 1.0 : creation
 */
public class ContactListAction implements ListSelectionListener {
	JList obj;
	MainFrame win;
	ContactController ctrl;
	
	/**
	 * default constructor
	 * @param obj the JList object which is displayed on the frame
	 * @param ctrl the main controller of the application
	 * @param win the main frame 
	 */
	public ContactListAction(JList obj, ContactController ctrl, MainFrame win) {
		super();
		this.obj = obj;
		this.ctrl = ctrl;
		this.win = win;
	}
	
	/**
	 * method called whenever the selected value changed in the list
	 */
	@Override
	public void valueChanged(ListSelectionEvent e) {
		// TODO Auto-generated method stub
		if (e.getValueIsAdjusting() == false) {
			System.out.println("selected : " + obj.getSelectedIndex() + " - "
					+ obj.getSelectedValue());
			if(obj.getSelectedIndex()==-1){
				win.changeDefaultView();
				ctrl.setCurrentSelected(-1);
			}
				
			else {
				win.changeContactView(ctrl.getContact(obj.getSelectedIndex()));
				ctrl.setCurrentSelected(obj.getSelectedIndex());
			}	
		}
	}
}
